package ntd.util.modular.data.util;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import ntd.util.config.common.exception.ComException;
import ntd.util.config.common.enums.data.EnumProtocolType;
import ntd.util.modular.data.util.handle.ComDataGetRule;
import ntd.util.modular.data.util.handle.ComDataGetRuleHttp;
import ntd.util.modular.data.util.handle.ComDataGetRuleTcp212;
import ntd.util.modular.data.util.handle.ComDataGetRuleTcp;

import java.util.Map;

public class FactoryHandleRule {

    private static final Map<EnumProtocolType, Class<? extends ComDataGetRule>> map = MapUtil.newHashMap();

    static {
        map.put(EnumProtocolType.HTTP, ComDataGetRuleHttp.class);
        map.put(EnumProtocolType.TCP, ComDataGetRuleTcp.class);
        map.put(EnumProtocolType.TCP_212, ComDataGetRuleTcp212.class);
    }

    public static ComDataGetRule get(EnumProtocolType enumProtocolType) {
        if (ObjectUtil.isNull(enumProtocolType)) {
            throw new ComException("协议不能为空！！");
        }
        if (!map.containsKey(enumProtocolType)) {
            throw new ComException("找不到对应数据采集规则处理方式[{}]", enumProtocolType.name());
        }
        return ReflectUtil.newInstance(map.get(enumProtocolType));
    }
}
